﻿//==========================================================
//Đã sửa bởi tranninh_86_yb@yahoo.com ngày 14/09/2013
//==========================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace prjKQXS_DLL.Hop_dong
{
    public class DMHOIDONG_DAL
    {
        public DataTable getAllHoiDong(bool isPublic)
        {
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection(BienTc.connString);
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * from DMHOIDONG where isPublic = @isPublic",conn);
                da.SelectCommand.Parameters.AddWithValue("@isPublic", isPublic);
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        public int InsertDMHOIDONG(DMHOIDONG dmhoidong)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            conn.Open();
            SqlCommand SqlCommand = new SqlCommand("insert into [DMHOIDONG] (Mahoidong,Tenhoidong,Chucvu,SDT,isPublic) values (@Mahoidong, @Tenhoidong, @Chucvu, @SDT, @isPublic)", conn);
            try
            {
                SqlCommand.Parameters.AddWithValue("@Mahoidong", dmhoidong.Mahoidong);
                SqlCommand.Parameters.AddWithValue("@Tenhoidong", dmhoidong.Tenhoidong);
                SqlCommand.Parameters.AddWithValue("@Chucvu", dmhoidong.Chucvu);
                SqlCommand.Parameters.AddWithValue("@SDT", dmhoidong.SDT);
                SqlCommand.Parameters.AddWithValue("@isPublic", dmhoidong.isPublic);
                return SqlCommand.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                SqlCommand.Dispose();
                conn.Close();
                conn.Dispose();
            }

        }
        public bool checkMahoidong(string mahoidong)
        {
            bool kq = false;
            SqlConnection conn = new SqlConnection(BienTc.connString);
            try
            {
                SqlCommand cmd = new SqlCommand("Select count(*) from DMHOIDONG where Mahoidong = @Mahoidong", conn);
                cmd.Parameters.AddWithValue("@Mahoidong", mahoidong);
                conn.Open();
                if (cmd.ExecuteScalar().ToString() == "0")
                {
                    kq = true;
                }
                else
                {
                    kq = false;
                }
            }
            catch (Exception)
            {
                kq = false;
            }
            return kq;
        }
        //public bool checkMa_DT(string ma_dt)
        //{
        //    bool retVal = false;
        //    SqlConnection conn = new SqlConnection(BienTc.connstring);
        //    try
        //    {
        //        SqlCommand cmd = new SqlCommand("Select count(*) from DMDONVI where MA_DT = @MA_DT", conn);
        //        cmd.Parameters.AddWithValue("@MA_DT", ma_dt);
        //        conn.Open();
        //        if (cmd.ExecuteScalar().ToString() == "0")
        //        {
        //            retVal = true;
        //        }
        //        else
        //        {
        //            retVal = false;
        //        }
        //    }
        //    catch (Exception)
        //    {
        //        retVal = false;
        //    }
        //    finally
        //    {
        //    }
        //    return retVal;
        //}
    }
}
